package com.daxt.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.annotations.LoginAuth;
import com.daxt.common.annotations.LoginUser;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.ArchivesFondsMapper;
import com.daxt.mapper.sys.WaterMarkMapper;
import com.daxt.model.service.archives.vo.ArchivesFonds;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.watermark.WaterMarkConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "水印信息")
@RestController
public class WaterMarkController {
    @Autowired
    private WaterMarkMapper WaterMarkMapper;
    @Autowired
    private ArchivesFondsMapper ArchivesFondsMapper;
    @ApiOperation(value = "查询", notes = "")
    @GetMapping("/watermark")
    @LoginAuth()
    public Result<List<WaterMarkConfig>> findAll(@ApiIgnore @LoginUser UserInfo loginUser,@ApiParam("全宗id")@RequestParam(value = "fondsId", required = false)String fondsId ) {
        QueryWrapper<WaterMarkConfig> queryWrapper =new QueryWrapper<>();
        queryWrapper.orderByAsc("id");
        if(!"admin".equals(loginUser.getUsername())&& !"超级管理员".equals(loginUser.getRole())) {
            queryWrapper.eq(StringUtils.isNotEmpty(loginUser.getFondsId()),"fondsId",loginUser.getFondsId());
        }else{
            queryWrapper.eq(StringUtils.isNotEmpty(fondsId),"fondsId",fondsId);
        }

        List<WaterMarkConfig>  list=WaterMarkMapper.selectList(queryWrapper);
        for(int i=0;i<list.size();i++){
            String fondsId2=list.get(i).getFondsId();
            if(StringUtils.isNotEmpty(fondsId2)){
               ArchivesFonds fonds= ArchivesFondsMapper.selectById(fondsId2);
               list.get(i).setFondsName(fonds.getFondsName());
            }
        }

        return  ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
    }

    @ApiOperation(value = "添加", notes = "")
    @PostMapping("/watermark")
    public Result<String> save(@RequestBody WaterMarkConfig entity) {
        int count=getFondsCount(entity);
        if(count>0){
            return ResultUtil.error("全宗不能重复！");
        }
        int rows=WaterMarkMapper.insert(entity);
        return ResultUtil.result(rows);
    }

    @ApiOperation(value = "修改", notes = "")
    @PutMapping("/watermark")
    public Result<String> edit(@RequestBody WaterMarkConfig entity) {
        WaterMarkMapper.equals(entity);
        int rows=WaterMarkMapper.updateById(entity);
        return ResultUtil.result(rows);
    }

    @ApiOperation(value = "删除", notes = "")
    @DeleteMapping("/watermark/{id}")
    public Result<String> del(@PathVariable("id") String id) {
        int rows=WaterMarkMapper.deleteById(id);
        return ResultUtil.result(rows);
    }

    private int getFondsCount(WaterMarkConfig entity){
        int count=0;
        if(StringUtils.isNotEmpty(entity.getFondsId())){
            QueryWrapper<WaterMarkConfig> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("fondsId",entity.getFondsId());
            count= WaterMarkMapper.selectCount(queryWrapper);
        }
        return count;
    }
}
